import { Controller } from 'egg';

class CloundController extends Controller {
    /**
     * 获取当前用户与目标用户的聊天记录（按时间先后顺序排）
     */
    public async getChatLog() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
                user_id: 'string',
                target_user_id: 'string',
                dateTime: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const page = Number(request.body.page);
        const size = Number(request.body.size);
        if (!page || page <= 0) {
            ctx.body = ctx.helper.responseErrorParam('页码格式错误');
            return;
        }
        if (!size || page <= 0) {
            ctx.body = ctx.helper.responseErrorParam('显示数量格式错误');
            return;
        }
        const result = await ctx.service.cloundService.getChatLog(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }

    /**
     * 获取聊天好友列表
     */
    public async getCloundFriend() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.cloundService.getCloundFriend(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }

    /**
     * 获取融云key
     */
    public async getCloundKey() {
        const { ctx } = this;
        const result = await ctx.service.cloundService.getCloundKey();
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }
}
export default CloundController;